﻿using System;

namespace TfsUtils.Ui.Mvvm.Command
{
	public abstract class AbstractMonitor<T> : IMonitor<T>
		where T : class
	{
		public event EventHandler Changed;

		protected virtual void OnChanged()
		{
			if( this.Changed != null )
			{
				this.Changed( this, EventArgs.Empty );
			}
		}

		public void NotifyChanged()
		{
			this.OnChanged();
		}

		protected AbstractMonitor( T source )
		{
			this.WeakSource = new WeakReference<T>( source );
		}

		protected WeakReference<T> WeakSource
		{
			get;
			private set;
		}

		public T Source
		{
			get { return this.WeakSource.Target; }
		}

		public void StopMonitoring()
		{
			this.OnStopMonitoring();
			this.WeakSource = null;
		}

		protected abstract void OnStopMonitoring();
	}
}
